package com.synnex.mobile.servier.core.contact.dao.impl;

import com.synnex.mobile.generic.dao.impl.GenericHibernateDaoImpl;
import com.synnex.mobile.servier.core.contact.dao.ContactDao;
import com.synnex.mobile.servier.core.contact.dto.Contact;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kerryf
 * Date: 13-8-5
 * To change this template use File | Settings | File Templates.
 */
public class ContactDaoImpl extends GenericHibernateDaoImpl<Contact, Integer> implements ContactDao {

    public List<Contact> ListContact(String key) {
        Criteria c = this.getSession().createCriteria(Contact.class);

        if(key!=null && !"".equals(key)){
            c.add(Restrictions.like("name",key, MatchMode.ANYWHERE));
            c.add(Restrictions.like("company",key, MatchMode.ANYWHERE));
        }
        c.addOrder(Order.asc("seq"));
        return c.list();
    }

    public void addContact(Contact record) {
        super.save(record);
    }

    public void editContact(Contact record) {
        super.update(record);
    }

    public Contact getContactById(Integer id) {
        return super.get(id);
    }

    public void deleteContactById(Integer id) {
        super.delete(super.get(id));
    }
}
